{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0711452d-3c9a-43b1-993b-1277043f9b52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([60000, 28, 28])\n",
      "epoch: 0, loss: 1.9793, acc: 0.6237\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from torchvision import datasets, transforms\n",
    "\n",
    "\n",
    "batch_size = 2048\n",
    "device = torch.device('cpu')\n",
    "\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(\n",
    "    datasets.MNIST('data', train=True, download=True,\n",
    "                   transform=transforms.Compose([\n",
    "                       transforms.ToTensor(),\n",
    "                   ])),\n",
    "    batch_size=batch_size, shuffle=True)\n",
    "\n",
    "test_loader = torch.utils.data.DataLoader(\n",
    "    datasets.MNIST('data', train=False,\n",
    "                   transform=transforms.Compose([\n",
    "                       transforms.ToTensor(),\n",
    "                   ])),\n",
    "    batch_size=batch_size, shuffle=True)\n",
    "\n",
    "print(train_loader.dataset.data.shape)\n",
    "\n",
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(MLP, self).__init__()\n",
    "        self.l1 = nn.Linear(784, 128)\n",
    "        self.l2 = nn.Linear(128, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        a1 = self.l1(x)\n",
    "        x1 = F.relu(a1)\n",
    "        a2 = self.l2(x1)\n",
    "        x2 = a2\n",
    "        return x2\n",
    "model = MLP().to(device)\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.05)\n",
    "model\n",
    "\n",
    "epochs = 1\n",
    "for epoch in range(epochs):\n",
    "    model.train()\n",
    "    for batch_idx, (x, y) in enumerate(train_loader):\n",
    "        x = x.view(x.size(0), -1).to(device)\n",
    "        y = y.to(device)\n",
    "        optimizer.zero_grad()\n",
    "        output = model(x)\n",
    "        loss = F.cross_entropy(output, y)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "    model.eval()\n",
    "    correct = 0\n",
    "    test_loss = 0\n",
    "    with torch.no_grad():\n",
    "        for batch_idx, (x, y) in enumerate(test_loader):\n",
    "            x = x.view(x.shape[0], -1).to(device)\n",
    "            y = y.to(device)\n",
    "            output = model(x)\n",
    "            test_loss += F.cross_entropy(output, y)\n",
    "            pred = output.max(1, keepdim=True)[1]\n",
    "            correct += pred.eq(y.view_as(pred)).sum().item()\n",
    "\n",
    "    test_loss = test_loss/(batch_idx+1)\n",
    "    acc = correct / len(test_loader.dataset)\n",
    "    print('epoch: {}, loss: {:.4f}, acc: {:.4f}'.format(epoch, test_loss, acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbf21111-f95f-4e53-9d71-f0806c65277b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acf67466-acc0-4145-bccd-43001e9a2ef4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:my_env]",
   "language": "python",
   "name": "conda-env-my_env-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
